import { uEditUserNearName } from "./../../../api/user";

import { uSelectUserInfo } from '@/api/user';
import { getStorage } from '@/utils/localstoreage';
import { observable, computed, action } from 'mobx'
import { createContext } from 'react';
import Taro from "@tarojs/taro";

class Editnearname {
  @observable pageData: {
    devCount: number
    user: UserSetting.UserInfo | null
  } = {
    devCount: 0,
    user: null
  }
  @action.bound
  async updatePageDataSource() {
    let userId = getStorage('userId')
    if (!userId) return
    let res = await uSelectUserInfo({userId})
    if (res && res.result === 'success') {
      this.pageData = res.data
    } else {
      this.pageData = {
        devCount: 0,
        user: null
      }
    }
  }

  @action.bound
  async formSubmit({nearName}: {nearName: string}) {
    let userId = getStorage('userId')
    if (!userId) return
    let res = await uEditUserNearName({userId, userNearName: nearName})
    if (res) {
      if (res.result === 'success') {
        Taro.showToast({
          icon: 'success',
          title: '修改成功'
        })
      } else if (res.result === 'error' && res.msg) {
        Taro.showToast({
          icon: 'error',
          title: res.msg
        })
      }
    }
  }
}
export const editNearNameStore = createContext(new Editnearname())
